package com.student.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.student.DBConn;
import com.student.entities.GoodsEntity;

public class RecommendService {

	public static List<GoodsEntity> getRecommendGoods(String userName) {
		List<GoodsEntity> goodsList = new ArrayList<GoodsEntity>();
		Connection conn = DBConn.getConn();
		Statement stmt;
		try {
			stmt = conn.createStatement();
			if (userName == null) {
				userName = "%";
			}
			String sql = "select * from shop_recommend,shop_goods,shop_seller where goods_sellerId=seller_id and  rcmd_goodsName = goods_name and rcmd_userName like '"
					+ userName
					+ "' order by rcmd_score desc,rcmd_count desc,rcmd_time desc limit 10";

			System.out.println(sql);
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				GoodsEntity goods = new GoodsEntity();
				goods.setState(rs.getInt("goods_state"));
				goods.setId(rs.getInt("goods_id"));
				goods.setClassId(rs.getInt("goods_classId"));
				goods.setName(rs.getString("goods_name"));
				goods.setSellerId(rs.getInt("goods_sellerId"));
				goods.setSaleClass(rs.getInt("goods_saleClass"));
				goods.setPrice(rs.getDouble("goods_price"));
				goods.setDescrition(rs.getString("goods_desc"));
				goods.setDiscount(rs.getDouble("goods_discount"));
				goods.setImageUrl(rs.getString("goods_image"));
				goods.setSellerName(rs.getString("seller_name"));
				goods.setRank(rs.getDouble("goods_rank"));
				goods.setSaleCount(rs.getInt("goods_saleCount"));
				goods.setNutrition(rs.getString("goods_nutrition"));
				goods.setKeyWords(rs.getString("goods_keywords"));
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				goodsList.add(goods);
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return goodsList;

	}

	public static void main(String[] args) {
		System.out.println(getRecommendGoods(null));
	}
}
